import type { App } from "vue";
import {
  createRouter,
  createWebHashHistory,
  RouteRecordRaw,
  createWebHistory,
} from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { title: "登录", hidden: true },
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/admin/dashboard",
    children: [
      {
        path: "/admin/dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        // 用于 keep-alive 功能，需要与 SFC 中自动推导或显式声明的组件名称一致
        // 参考文档: https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        name: "Dashboard",
        meta: {
          title: "首页",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "/401",
        component: () => import("@/views/error-page/401.vue"),
        meta: { hidden: true },
      },
    ],
  },

  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: { title: "404", hidden: true },
  },
  // {
  //   path: "/study",
  //   component: Layout,
  //   name: "study",
  //   meta: { title: "在线学习", icon: "study" },
  //   redirect: "/admin/dashboard",
  //   children: [
  //     {
  //       path: "/user/study/article",
  //       name: "studyArticle",
  //       component: () => import("@/views/study/article/index.vue"),
  //       meta: { title: "浏览文章", icon: "article", keepAlive: true },
  //     },
  //     {
  //       path: "/user/study/article/:id",
  //       name: "detail",
  //       component: () => import("@/views/study/article/detail.vue"),
  //       meta: { title: "浏览文章", hidden: true },
  //     },
  //     {
  //       path: "/user/study/homework",
  //       name: "homework",
  //       component: () => import("@/views/study/homework/index.vue"),
  //       meta: { title: "习题练习", icon: "homework", keepAlive: true },
  //     },
  //   ],
  // },
  // {
  //   path: "/user",
  //   component: Layout,
  //   name: "users",
  //   redirect: "/user/article",
  //   children: [
  //     {
  //       path: "/user/article",
  //       name: "article",
  //       component: () => import("@/views/menu/article/index.vue"),
  //       meta: { title: "考核评价", icon: "article", keepAlive: true },
  //     },
  //   ],
  // },
  // {
  //   path: "/user/editUser",
  //   name: "editUser",
  //   component: Layout,
  //   children: [
  //     {
  //       path: "/user/editUser",
  //       name: "editUser",
  //       component: () => import("@/views/editUser/index.vue"),
  //       meta: { title: "修改信息", icon: "user", keepAlive: true },
  //     },
  //   ],
  // },
  // {
  //   path: "/admin/users",
  //   name: "users",
  //   component: Layout,
  //   children: [
  //     {
  //       path: "/admin/users",
  //       name: "users",
  //       component: () => import("@/views/users/index.vue"),
  //       meta: { title: "用户管理", icon: "user", keepAlive: true },
  //     },
  //   ],
  // },
  // {
  //   path: "/admin/menu",
  //   component: Layout,
  //   name: "menu",
  //   component: () => import("@/views/menu/index.vue"),
  //   meta: { title: "文章管理", icon: "menu" },
  //   children: [
  //     {
  //       path: "/admin/article",
  //       name: "article",
  //       component: () => import("@/views/menu/article/index.vue"),
  //       meta: { title: "文章列表", icon: "article", keepAlive: true },
  //     },
  //     {
  //       path: "/admin/category",
  //       name: "category",
  //       component: () => import("@/views/menu/category/index.vue"),
  //       meta: { title: "类别管理", icon: "article", keepAlive: true },
  //     },
  //   ],
  // },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: "/login" });
}

export default router;
